package org.jivesoftware.smack.proxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.SocketFactory;

public class Socks4ProxySocketFactory extends SocketFactory
{
  private ProxyInfo proxy;

  public Socks4ProxySocketFactory(ProxyInfo paramProxyInfo)
  {
    this.proxy = paramProxyInfo;
  }

  public Socket createSocket(String paramString, int paramInt)
    throws IOException, UnknownHostException
  {
    return socks4ProxifiedSocket(paramString, paramInt);
  }

  public Socket createSocket(String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2)
    throws IOException, UnknownHostException
  {
    return socks4ProxifiedSocket(paramString, paramInt1);
  }

  public Socket createSocket(InetAddress paramInetAddress, int paramInt)
    throws IOException
  {
    return socks4ProxifiedSocket(paramInetAddress.getHostAddress(), paramInt);
  }

  public Socket createSocket(InetAddress paramInetAddress1, int paramInt1, InetAddress paramInetAddress2, int paramInt2)
    throws IOException
  {
    return socks4ProxifiedSocket(paramInetAddress1.getHostAddress(), paramInt1);
  }

  private Socket socks4ProxifiedSocket(String paramString, int paramInt)
    throws IOException
  {
    Socket localSocket = null;
    InputStream localInputStream = null;
    OutputStream localOutputStream = null;
    String str1 = this.proxy.getProxyAddress();
    int i = this.proxy.getProxyPort();
    String str2 = this.proxy.getProxyUsername();
    String str3 = this.proxy.getProxyPassword();
    try
    {
      localSocket = new Socket(str1, i);
      localInputStream = localSocket.getInputStream();
      localOutputStream = localSocket.getOutputStream();
      localSocket.setTcpNoDelay(true);
      byte[] arrayOfByte1 = new byte[1024];
      int j = 0;
      j = 0;
      arrayOfByte1[(j++)] = 4;
      arrayOfByte1[(j++)] = 1;
      arrayOfByte1[(j++)] = (byte)(paramInt >>> 8);
      arrayOfByte1[(j++)] = (byte)(paramInt & 0xFF);
      int n;
      try
      {
        InetAddress localInetAddress = InetAddress.getByName(paramString);
        byte[] arrayOfByte2 = localInetAddress.getAddress();
        for (n = 0; n < arrayOfByte2.length; n++)
          arrayOfByte1[(j++)] = arrayOfByte2[n];
      }
      catch (UnknownHostException localUnknownHostException)
      {
        throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, localUnknownHostException.toString(), localUnknownHostException);
      }
      if (str2 != null)
      {
        System.arraycopy(str2.getBytes(), 0, arrayOfByte1, j, str2.length());
        j += str2.length();
      }
      arrayOfByte1[(j++)] = 0;
      localOutputStream.write(arrayOfByte1, 0, j);
      int k = 6;
      int m = 0;
      while (m < k)
      {
        n = localInputStream.read(arrayOfByte1, m, k - m);
        if (n <= 0)
          throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, "stream is closed");
        m += n;
      }
      if (arrayOfByte1[0] != 0)
        throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, "server returns VN " + arrayOfByte1[0]);
      if (arrayOfByte1[1] != 90)
      {
        try
        {
          localSocket.close();
        }
        catch (Exception localException3)
        {
        }
        localObject = "ProxySOCKS4: server returns CD " + arrayOfByte1[1];
        throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, (String)localObject);
      }
      Object localObject = new byte[2];
      localInputStream.read((byte[])localObject, 0, 2);
      return localSocket;
    }
    catch (RuntimeException localRuntimeException)
    {
      throw localRuntimeException;
    }
    catch (Exception localException1)
    {
      try
      {
        if (localSocket != null)
          localSocket.close();
      }
      catch (Exception localException2)
      {
      }
      throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, localException1.toString());
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.proxy.Socks4ProxySocketFactory
 * JD-Core Version:    0.6.1
 */